/**
 * 
 */
package com.keggview.application.xml.loader;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.keggview.application.datatypes.Alt;
import com.keggview.application.datatypes.Base;
import com.keggview.application.datatypes.Component;
import com.keggview.application.datatypes.Entry;
import com.keggview.application.datatypes.Graphics;
import com.keggview.application.datatypes.Pathway;
import com.keggview.application.datatypes.Product;
import com.keggview.application.datatypes.Reaction;
import com.keggview.application.datatypes.Registry;
import com.keggview.application.datatypes.Relation;
import com.keggview.application.datatypes.Substrate;
import com.keggview.application.datatypes.Subtype;

/**
 * @author Pablo
 *
 */
public class XMLReader {
	
	private Document xmlDoc = null;
	
	private FileWriter fw;
	
	public XMLReader(String filePath) {
		File openFile = new File(filePath);
		try {
			fw = new FileWriter("c:\\dump.txt");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(openFile);
			this.xmlDoc = document;
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public void treeWalk(Document document) {
		if (document == null) {
			document = this.xmlDoc;
		}
//		treeWalk( document.getRootElement() );
		ObjectFactory.lookFor(document.getRootElement(), "reaction");
	}

	public void treeWalk(Element element) {
    	String name = element.getName();
    	Base object = null;
    	if (name.equals("alt")) {
    		Alt alt = new Alt(element);
    		object = alt;
    	}
    	if (name.equals("component")) {
    		Component comp = new Component(element);
    		object = comp;
    	}
    	if (name.equals("entry")) {
    		Entry entry = new Entry(element);
    		object = entry;
    	}
    	if (name.equals("graphics")) {
    		Graphics graphics = new Graphics(element);
    		object = graphics;
    	}
    	if (name.equals("pathway")) { 
    		Pathway pathway = new Pathway(element);
    		object = pathway; 
    	}
    	if (name.equals("product")) { 
    		Product p = new Product(element);
    		object = p; 
    	}
    	if (name.equals("reaction")) { 
    		Reaction p = new Reaction(element);
    		object = p; 
    	}
    	if (name.equals("relation")) { 
    		Relation p = new Relation(element);
    		object = p; 
    	}
    	if (name.equals("substrate")) {
    		Substrate p = new Substrate(element);
    		object = p;
    	}
    	if (name.equals("subtype")) {
    		Subtype p = new Subtype(element);
    		object = p;
    	}
    	Registry.register(object);
        for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
            Node node = element.node(i);
            if ( node instanceof Element ) {
                treeWalk( (Element) node );
            }
            else {
                ; // value isn't a Element. Nothing interesting
            }
            try {
				fw.write(node.getNodeTypeName() + "\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
    
    
    
    /**
	 * @return the xmlDoc
	 */
	public Document getXmlDoc() {
		return xmlDoc;
	}

	/**
	 * @param xmlDoc the xmlDoc to set
	 */
	public void setXmlDoc(Document xmlDoc) {
		this.xmlDoc = xmlDoc;
	}
	
	public void close() {
		try {
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		XMLReader xml = new XMLReader("d:\\mgr\\maps\\map00350.xml");
		Document doc = xml.getXmlDoc();
		xml.treeWalk(doc);
		xml.close();
	}

}
